package com.cn.web.controller.Equipment;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.pojo.lbj.ComponentSingleRegistrationTable;
import com.cn.bean.pojo.lbj.SingleParameterItemCurrentDataTable;
import com.cn.bean.pojo.workOrder.*;
import com.cn.config.config.Result;
import com.cn.service.server.ComponentMechanical.OutsourcingResultService;
import com.cn.service.server.Equipment.GrindingResultService;
import com.cn.service.server.Equipment.WorkOrderPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class GrindingResultController {
    @Autowired
    private GrindingResultService grindingResultService;
    @Autowired
    private WorkOrderPlanService workOrderPlanService;
    @Autowired
    private OutsourcingResultService outsourcingResultService;
    @RequestMapping("/select/GrindingResult")
    @ResponseBody
    public List<GrindingResultMainTable> selectGrindingResult(@RequestBody GrindingResultMainTable grindingResultMainTable) {
        List<GrindingResultMainTable> assists = grindingResultService.selectGrindingResultMain(grindingResultMainTable);
        return assists;
    }
    @RequestMapping("/select/GrindingResultSub")
    @ResponseBody
    public List<GrindingResultSubTable> selectGrindingResult(@RequestBody GrindingResultSubTable grindingResultSubTable) {
        List<GrindingResultSubTable> assists = grindingResultService.selectGrindingResultSub(grindingResultSubTable);
        return assists;
    }
    @RequestMapping("/select/GrindingResult/All")
    @ResponseBody
    public List<GrindingResultMainTable> selectGrindingResultAll(@RequestBody GrindingResultMainTable grindingResultMainTable) {
        List<GrindingResultMainTable> assists = grindingResultService.selectGrindingResultMainAll(grindingResultMainTable);
        return assists;
    }
    @RequestMapping("/select/GrindingResult/Al")
    @ResponseBody
    public List<GrindingResultMainTable> selectGrindingResultAl(@RequestBody GrindingResultMainTable grindingResultMainTable) {
        List<GrindingResultMainTable> assists = grindingResultService.selectGrindingResultMainAl(grindingResultMainTable);
        return assists;
    }
    @RequestMapping("/select/GrindingWorker")
    @ResponseBody
    public List<User> selectGrindingResult(@RequestBody User user) {
        List<User> assists = grindingResultService.selectWorker(user);
        return assists;
    }
    @RequestMapping("/select/GrindingWorkOrder")
    @ResponseBody
    public List<BusinessItemWorkOrderPool> selectGrindingWorkOrder(@RequestBody JSONObject jsonObject) {
        String workOrderCode = jsonObject.getString("input1");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        workOrderCode = (workOrderCode != null) ? workOrderCode : "";
       return grindingResultService.selectGrindingWorkOrder(workOrderCode, page, limit);
    }
    @RequestMapping("/select/ComponentSingle")
    @ResponseBody
    public List<ComponentSingleRegistrationTable> selectComponentSingle(@RequestBody JSONObject jsonObject) {
        String uniqueCode = jsonObject.getString("input1");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        uniqueCode = (uniqueCode != null) ? uniqueCode : "";
        return grindingResultService.selectComponentSingleMain(page, limit,uniqueCode);
    }
    @RequestMapping("/select/ComponentSingle/A")
    @ResponseBody
    public List<ComponentSingleRegistrationTable> selectComponentSingleA(@RequestBody JSONObject jsonObject) {
        String uniqueCode = jsonObject.getString("uniqueCode");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        uniqueCode = (uniqueCode != null) ? uniqueCode : "";
        return grindingResultService.selectComponentSingleMain(page, limit,uniqueCode);
    }
    @RequestMapping("/select/ComponentSingle/All")
    @ResponseBody
    public List<ComponentSingleRegistrationTable> selectComponentSingleAll(@RequestBody ComponentSingleRegistrationTable componentSingleRegistrationTable) {
        return grindingResultService.selectComponentSingleMainAll(componentSingleRegistrationTable);
    }

    @RequestMapping("/select/CurrentDataTable")
    @ResponseBody
    public List<SingleParameterItemCurrentDataTable> selectCurrentDataTable(@RequestBody SingleParameterItemCurrentDataTable singleParameterItemCurrentDataTable) {

        List<SingleParameterItemCurrentDataTable> sub = grindingResultService.selectCurrentDataTable(singleParameterItemCurrentDataTable);
        return sub;
    }

    @RequestMapping("/insert/GrindingWorkOrder")
    @ResponseBody
    public Result insertGrindingWorkOrder(@RequestBody JSONObject jsonObject) {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("childList");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        GrindingResultMainTable mainTable = new GrindingResultMainTable();
        Integer id = grindingResultService.getGrindingWorkOrderAutoid();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String userName = jsonObject.getString("userName");
        int grindingStandardMid=jsonObject.getInteger("grindingStandardMid");
        int workOrderNumber=jsonObject.getInteger("workOrderNumber");
       int processingWorkerId=jsonObject.getInteger("processingWorkerId");;
      int processingWorkTeamId=jsonObject.getInteger("processingWorkTeamId");;
      int processingShift=jsonObject.getInteger("processingShift");;
       String processingDay=jsonObject.getString("processingDay");;
        String signReason="本次磨削量大于单次磨削量";
        mainTable.setId(id);
        mainTable.setGrindingStandardMid(grindingStandardMid);
        mainTable.setWorkOrderNumber(workOrderNumber);
        mainTable.setCjr(userName);
        mainTable.setCjsj(format.format(date));
        mainTable.setDjNumber(id);
        mainTable.setDjTime(format.format(date));
        mainTable.setDjStatus(0);
        mainTable.setSignReason(signReason);
        mainTable.setProcessingWorkTeamId(processingWorkTeamId);
        mainTable.setProcessingWorkerId(processingWorkerId);
        mainTable.setProcessingShift(processingShift);
        mainTable.setProcessingDay(processingDay);
        List<GrindingResultMainTable>mainTables=grindingResultService.selectGrindingResultMainA(workOrderNumber);
        if (mainTables.size()>0){
            return Result.failure("保存失败，已经存在相同单据！");
        }
        int a = grindingResultService.insertGrindingResultMain(mainTable);
        if (a != 1) {
            return Result.failure("保存失败，有数据不符合主表");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<GrindingResultSubTable> djBhList = dataArray.toJavaList(GrindingResultSubTable.class);
            for (GrindingResultSubTable dataStructureDto : djBhList) {
                GrindingResultSubTable subTable = new GrindingResultSubTable();
                SingleParameterItemCurrentDataTable currentDataTable = new SingleParameterItemCurrentDataTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = grindingResultService.getGrindingResultSubTableAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                if (subTable.getGrindStandard().compareTo(subTable.getBcGrindingCapacity())==-1){
                    subTable.setIsSign(1);
                    subTable.setSignReason(signReason);
                }
                currentDataTable.setPartSingleID(subTable.getUniqueCode());
                currentDataTable.setTotalGrindingAmount(subTable.getTotalGrindingAmount());
                currentDataTable.setResidualGrindingCapacity(subTable.getResidualGrindingCapacity());
                currentDataTable.setRoughnessStandard(subTable.getRoughnessStandard());
                currentDataTable.setLJGrindingTimes(subTable.getLJGrindingTimes()+1);
                int b = grindingResultService.insertGrindingResultSub(subTable);
                int d=grindingResultService.updateCurrentDataTable(currentDataTable);
                if (b != 1&&d!=1) {
                    return Result.failure("保存失败，有数据不符合子表数据");
                }
            }
        }
        return Result.success("200","添加成功");
    }
    @RequestMapping("/update/GrindingWorkOrder")
    @ResponseBody
    public Result updateGrindingWorkOrder(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("childList");
        Integer id =jsonObject.getInteger("id");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<GrindingResultSubTable>subTableList=grindingResultService.selectGrindingSub(id);
        if (subTableList.size()>0){
            int v=grindingResultService.delGrindingSub(id);
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<GrindingResultSubTable> djBhList = dataArray.toJavaList(GrindingResultSubTable.class);
            for (GrindingResultSubTable dataStructureDto : djBhList) {
                GrindingResultSubTable subTable = new GrindingResultSubTable();
                SingleParameterItemCurrentDataTable currentDataTable = new SingleParameterItemCurrentDataTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = grindingResultService.getGrindingResultSubTableAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                currentDataTable.setPartSingleID(subTable.getUniqueCode());
                currentDataTable.setTotalGrindingAmount(subTable.getTotalGrindingAmount());
                currentDataTable.setResidualGrindingCapacity(subTable.getResidualGrindingCapacity());
                currentDataTable.setRoughnessStandard(subTable.getRoughnessStandard());
                currentDataTable.setLJGrindingTimes(subTable.getLJGrindingTimes()+1);
                int b = grindingResultService.insertGrindingResultSub(subTable);
                int d=grindingResultService.updateCurrentDataTable(currentDataTable);
                if (b != 1&&d!=1) {
                    return Result.failure("500");
                }
            }
        }
        return Result.success("200","添加成功");
    }
    @RequestMapping("/bf/Grinding")
    @ResponseBody
    public Result BfGrindingAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                grindingResultService.updateGrindingAudit(id);
                List<GrindingResultSubTable>subTableList=grindingResultService.selectGrindingSub(id);
                grindingResultService.BfComponentSingleStatus(subTableList.get(0).getUniqueCode());
            }
            return Result.success("200","报废成功");
        }
        return Result.failure("500", "报废失败");
    }
    @RequestMapping("/audit/Grinding")
    @ResponseBody
    public Result updateGrindingAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        JSONArray workArray = jsonObject.getJSONArray("workOrderCode");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            List<Integer> workList = workArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                List<GrindingResultSubTable>subTableList=grindingResultService.selectGrindingSub(id);
                for (GrindingResultSubTable subTable : subTableList) {
                    if (subTable.getIsSign()==1&&subTable.getSignStatus()==0){
                        return Result.failure("审核失败，存在没有签批的数据");
                    }
                    grindingResultService.updateComponentSingleRegistration(subTable.getUniqueCode());
                }
                grindingResultService.updateGrindingAudit(id);
                for (Integer workOrderNumber : workList) {
                    outsourcingResultService.completeOutsourcingWorkOrde(workOrderNumber);
                }
            }
            return Result.success("200","审核成功");
        }
        return Result.failure("500", "审核失败");
    }
    @RequestMapping("/del/Grinding")
    @ResponseBody
    public Result delGrindingAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        JSONArray workArray = jsonObject.getJSONArray("workOrderCode");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            List<Integer> workList = workArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                grindingResultService.delGrindingAudit(id);
//                for (Integer workOrderNumber : workList) {
//                    outsourcingResultService.completeOutsourcingWorkOrde(workOrderNumber);
//                }
            }
            return Result.success("200","审核成功");
        }
        return Result.failure("500", "审核失败");
    }
    @RequestMapping("/sign/GrindingSign")
    @ResponseBody
    public Result GrindingSign(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                grindingResultService.addGrindingSign(id);
                grindingResultService.addGrindingSubSign(id);
            }
            return Result.success("200","签批成功");
        }
        return Result.failure("500", "签批失败");
    }
    @RequestMapping("/del/GrindingSign")
    @ResponseBody
    public Result delGrindingSign(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                grindingResultService.delGrindingSign(id);
                grindingResultService.delGrindingSubSign(id);
            }
            return Result.success("200","弃签成功");
        }
        return Result.failure("500", "弃签失败");
    }

}
